﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UCLibrary.UC
{
    public partial class UCPipeV : UserControl
    {
        public UCPipeV()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            t.Tick += tTick;
            t.Enabled = true;
            t.Interval = 100;
        }
        private Color _PipeColor = Color.Gray;
        public Color PipeColor
        {
            get { return _PipeColor; }
            set { _PipeColor = value; Refresh(); }
        }

        public enum Area
        {
            left, right, line,joint,none
        }
        private Area _oneArea = Area.line;
        [Category("杂项"), Description("一区设定")]
        public Area oneArea
        {
            get { return _oneArea; }
            set { _oneArea = value; Refresh(); }
        }
        private Area _threeArea = Area.line;
        [Category("杂项"), Description("三区设定")]
        public Area threeArea
        {
            get { return _threeArea; }
            set { _threeArea = value; Refresh(); }
        }
        private bool _reversal = false;
        public bool reversal
        {
            get { return _reversal; }
            set { _reversal = value; Refresh(); }
        }
        private bool _isActive = true;
        public bool isActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                if (_isActive)
                    t.Enabled = true;
                else
                    t.Enabled = false;
                Refresh();
            }
        }
        Timer t = new Timer();
        private void tTick(object sender, EventArgs e)
        {
            x += -2 * Convert.ToInt16(reversal) + 1;
            if (x >= 10)
                x = 0;
            Refresh();
        }
        int x;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            GraphicsPath centerLine = new GraphicsPath();
            GraphicsPath RegionLine = new GraphicsPath();
            int halfWidth = Width / 2;
            //消除高无法整除时产生的锯齿
            Width = halfWidth * 2;
            if (Width > Height/2)
                Height = Width * 2;
            //一区划线
            switch(oneArea)
            {
                case Area.line :
                    centerLine.AddLine(halfWidth,0, halfWidth,Width);
                    RegionLine.AddRectangle(new RectangleF(0,-1,Width,Width + 1));
                    break;
                case Area.left:
                    centerLine.AddArc(new RectangleF(-halfWidth,halfWidth,Width,Width), -90, 90);
                    RegionLine.AddArc(new RectangleF(-Width,0,2*Width,2*Width),-90,90);
                    RegionLine.AddPolygon(new Point[] { new Point(Width, Width), new Point(0, Width), new Point(0, 0) });
                    break;

                case Area.right:
                    centerLine.AddArc(new RectangleF(halfWidth, halfWidth, Width, Width), -90, -90);
                    RegionLine.AddArc(new RectangleF(0, 0, 2 * Width, 2 * Width), 180, 90);
                    RegionLine.AddPolygon(new Point[] { new Point(Width, 0), new Point(Width, Width), new Point(0, Width) });
                    break;
                case Area.none:
                    break;

                case Area.joint:
                    RegionLine.AddRectangle(new RectangleF(0, 0, Width, Width));
                    break;
                default:
                    centerLine.AddLine(halfWidth, 0, halfWidth, Width);
                    break;

            }
            //二区划线
            centerLine.AddLine(halfWidth, Width, halfWidth, Height-Width);
            RegionLine.AddRectangle(new RectangleF(0, Width, Width, Height - 2 * Width));

            //三区划线
            switch (threeArea)
            {
                case Area.line:
                    centerLine.AddLine(halfWidth, Height - Width, halfWidth, Height);
                    RegionLine.AddRectangle(new RectangleF(0, Height-Width, Width, Width));
                    break;
                case Area.left:
                    centerLine.AddArc(new RectangleF(-halfWidth, Height-halfWidth-Width, Width, Width), 0, 90);
                    RegionLine.AddArc(new RectangleF(-Width, Height-2*Width, 2 * Width, 2 * Width), 0, 90);
                    RegionLine.AddPolygon(new Point[] { new Point(0, Height), new Point(0,Height - Width), new Point(Width, Height - Width) });
                    break;

                case Area.right:
                    centerLine.AddArc(new RectangleF(halfWidth, Height - halfWidth - Width, Width, Width), 180, -90);
                    RegionLine.AddArc(new RectangleF(0, Height - 2 * Width, 2 * Width, 2 * Width), 180, -90);
                    RegionLine.AddPolygon(new Point[] { new Point(Width, Height), new Point(Width, Height - Width), new Point(0, Height - Width) });
                    break;
                case Area.none:
                    break;
                case Area.joint:
                    RegionLine.AddRectangle(new RectangleF(0, Height - Width, Width, Width));
                    break;
                default:
                    break;

            }
            Region = new Region(RegionLine);
            g.FillPath(new SolidBrush(PipeColor), RegionLine);
            List<Color> listColor = new List<Color>();
            g.FillPath(new SolidBrush(PipeColor), RegionLine);
            Function.GradientRamp(PipeColor, Color.White, halfWidth, ref listColor);
            Pen p;
            for (int i = 0; i < listColor.Count; i++)
            {
                p = new Pen(new SolidBrush(listColor[i]), halfWidth - i);
                g.DrawPath(p, centerLine);
            }
            listColor.Clear();

            if (isActive)
            {
                p = new Pen(Brushes.Green, 4);
                p.DashStyle = DashStyle.Dash;
                p.DashPattern = new float[] { 5, 5 };
                p.DashOffset = x;
                g.DrawPath(p, centerLine);
            }
        }

        private void UCPipeV_Resize(object sender, EventArgs e)
        {
            Refresh();
        }
    }
}
